﻿using UnityEngine;
using System.Collections;
using admob;
public class 广告集成Old : MonoBehaviour
{
    static Admob ad = null;

    public int m_底部广告切换周期 = 50;
    public bool m_底部广告已显示 = false;
    private int m_上次玩家所在行数 = 0;
    public static 广告集成 m_当前广告集成;
    private string id_插页广告 = "ca-app-pub-3682424280630333/4084604307";
    private string id_底部广告 = "ca-app-pub-3682424280630333/1289251701";
    private string id_激励广告 = "ca-app-pub-3682424280630333/3190857865";
    private string id_原生中屏大广告 = "ca-app-pub-3682424280630333/2537556053";

    void Start()
    {
        Debug.Log("start unity demo-------------");
        initAdmob();

        if(m_当前广告集成 == null)
        {
            //m_当前广告集成 = this;
        }
    }

    private void FixedUpdate()
    {

        //if (Input.GetKeyUp(KeyCode.Escape))
        //{
        //    Debug.Log(KeyCode.Escape + "-----------------");
        //}

        if (m_上次玩家所在行数 != 地图管理器.m_当前地图管理器.m_玩家当前所在行)
        {
            m_上次玩家所在行数 = 地图管理器.m_当前地图管理器.m_玩家当前所在行;

            if ( (地图管理器.m_当前地图管理器.m_玩家当前所在行+ m_底部广告切换周期) % m_底部广告切换周期 == 5 )
            {
                Debug.Log("玩家当前所在行 " + 地图管理器.m_当前地图管理器.m_玩家当前所在行);
                m_底部广告已显示 = !m_底部广告已显示;

                if (m_底部广告已显示)
                {
                    显示底部广告();
                }
                else
                {
                    关闭底部广告();
                }
            }
        }
        else
        {
            m_上次玩家所在行数 = 地图管理器.m_当前地图管理器.m_玩家当前所在行;
        }

    }

    void initAdmob()
    {
        if (ad == null)
        {
            ad = Admob.Instance();
            ad.bannerEventHandler += onBannerEvent;
            ad.interstitialEventHandler += onInterstitialEvent;
            ad.rewardedVideoEventHandler += onRewardedVideoEvent;
            ad.nativeBannerEventHandler += onNativeBannerEvent;
            ad.initAdmob(id_底部广告, id_插页广告);//all id are admob test id,change those to your
                                           //ad.showBannerRelative(AdSize.Banner, AdPosition.BOTTOM_CENTER, 0);
                                           //ad.setTesting(true);//show test ad
            ad.setGender(AdmobGender.MALE);
            string[] keywords = { "game", "phone", "vr" };
            ad.setKeywords(keywords);//set keywords for ad
            Debug.Log("admob inited -------------");
        }
    }

    public void 显示底部广告()
    {
        Debug.Log("显示底部广告");
        Admob.Instance().showBannerRelative(AdSize.SmartBanner, AdPosition.BOTTOM_CENTER, 0);
    }
    public void 关闭底部广告()
    {
        Debug.Log("关闭底部广告");
        Admob.Instance().removeBanner();

    }
    public void 显示全屏广告()
    {
        Debug.Log("全屏广告");

        if (Admob.Instance().isInterstitialReady())
        {
            Admob.Instance().showInterstitial();
        }
        else
        {
            Admob.Instance().loadInterstitial();
        }
    }
    public void 显示视频广告()
    {
        Debug.Log("显示视频广告");

        if (Admob.Instance().isRewardedVideoReady())
        {
            Admob.Instance().showRewardedVideo();
        }
        else
        {
            Admob.Instance().loadRewardedVideo(id_激励广告);
        }
    }

    public void 显示暂停广告()
    {
        Debug.Log("显示暂停广告");
        Admob.Instance().showNativeBannerRelative(new AdSize(280,320) , AdPosition.MIDDLE_CENTER, 0, id_原生中屏大广告);
    }
    public void 关闭暂停广告()
    {
        Debug.Log("关闭暂停广告");
        Admob.Instance().removeNativeBanner();
    }
    public void 关闭广告()
    {
        Debug.Log("关闭广告");
        Admob.Instance().removeAllBanner();
    }

    //void OnGUI()
    //{
    //    if (GUI.Button(new Rect(120, 0, 100, 60), "showInterstitial"))
    //    {


    //    }

    //    if (GUI.Button(new Rect(240, 0, 100, 60), "showRewardVideo"))
    //    {

    //        if (ad.isRewardedVideoReady())
    //        {
    //            ad.showRewardedVideo();
    //        }
    //        else
    //        {
    //            ad.loadRewardedVideo(id_激励广告);
    //        }
    //    }

    //    if (GUI.Button(new Rect(0, 100, 100, 60), "showbanner"))
    //    {
    //        Admob.Instance().showBannerRelative(AdSize.SmartBanner, AdPosition.BOTTOM_CENTER, 0);
    //    }

    //    if (GUI.Button(new Rect(120, 100, 100, 60), "showbannerABS"))
    //    {
    //        Admob.Instance().showBannerAbsolute(AdSize.Banner, 20, 300);
    //    }

    //    if (GUI.Button(new Rect(240, 100, 100, 60), "removebanner"))
    //    {
    //        Admob.Instance().removeBanner();
    //    }

    //    string nativeBannerID = id_原生广告;
    //    if (GUI.Button(new Rect(0, 200, 100, 60), "showNative"))
    //    {
    //        Admob.Instance().showNativeBannerRelative(new AdSize(320, 120), AdPosition.BOTTOM_CENTER, 0, nativeBannerID);
    //    }

    //    if (GUI.Button(new Rect(120, 200, 100, 60), "showNativeABS"))
    //    {
    //        Admob.Instance().showNativeBannerAbsolute(new AdSize(320, 120), 20, 300, nativeBannerID);
    //    }

    //    if (GUI.Button(new Rect(240, 200, 100, 60), "removeNative"))
    //    {
    //        Admob.Instance().removeNativeBanner();
    //    }
    //}

    void onInterstitialEvent(string eventName, string msg)
    {
        Debug.Log("handler onAdmobEvent---" + eventName + "   " + msg);
        if (eventName == AdmobEvent.onAdLoaded)
        {
            Admob.Instance().showInterstitial();
        }
    }
    void onBannerEvent(string eventName, string msg)
    {
        Debug.Log("handler onAdmobBannerEvent---" + eventName + "   " + msg);
    }
    void onRewardedVideoEvent(string eventName, string msg)
    {
        Debug.Log("handler onRewardedVideoEvent---" + eventName + "  rewarded: " + msg);
    }
    void onNativeBannerEvent(string eventName, string msg)
    {
        Debug.Log("handler onAdmobNativeBannerEvent---" + eventName + "   " + msg);
    }
}
